package com.net.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.net.model.User;

public class AuthInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
        /*判断是否需要过滤的页面*/  
		System.out.println(request.getRequestURI());
		if("/network_monitoring_system/net/user/login".equals(request.getRequestURI()) || "/network_monitoring_system/src/main/webapp/static/js/jquery-2.1.1.min.js".equals(request.getRequestURI())){
			return true;
		}
        HttpSession session = request.getSession();    
          User user = (User) session.getAttribute("user");
        if(user == null){  
        	String contextPath = request.getContextPath();
            /*跳转到登录页*/  
            response.sendRedirect(contextPath + "/index");
            return false;  
        }
        return true;
	}
	
}
